package com.zxg.zbc.biz.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.imageio.ImageIO;

/**
 * 纯java处理图片拼接（背景上边添加文字和图片）
 * 
 * @author Administrator
 *
 */
public class ImageUtils {

  public static void drawPicture(int x, int y, int w, int h, BufferedImage background, BufferedImage small) {
    // int width = small.getWidth();
    // int height = small.getHeight();
    Image image = small.getScaledInstance(w, h, Image.SCALE_DEFAULT);
    BufferedImage oimage = new BufferedImage(w, h, Image.SCALE_DEFAULT);
    oimage.getGraphics().drawImage(image, 0, 0, null);
    int[] smallArr = new int[w * h];
    smallArr = oimage.getRGB(0, 0, w, h, smallArr, 0, w);
    background.setRGB(x, y, w, h, smallArr, 0, w);
  }

  public static void drawText(int x, int y, BufferedImage background, String content, String font, int fontSize, String color) {
    Graphics g = background.getGraphics();
    Font f = new Font(font, Font.PLAIN, fontSize);
    g.setColor(Color.decode(color));
    g.setFont(f);
    g.drawString(content, x, y);
    g.dispose();
  }

  public static void main(String[] args) throws IOException {
    // ImageUtils.makePicture();
    String front = "/Users/jsbd/Downloads/front.png", back = "/Users/jsbd/Downloads/back.png", qrcode = "/Users/jsbd/Downloads/qrcode.png",
        taobao = "/Users/jsbd/Downloads/taobao.jpg", resFront = "/Users/jsbd/Downloads/resFront.png";
    BufferedImage frontImg = ImageIO.read(new File(front));
    BufferedImage backImg = ImageIO.read(new File(back));
    BufferedImage qrcodeImg = ImageIO.read(new File(qrcode));
    BufferedImage taobaoImg = ImageIO.read(new File(taobao));

    ImageUtils.drawPicture(1180, 211, 422, 422, frontImg, qrcodeImg);
//    ImageUtils.drawText(931, 1682, frontImg, "这里是内容", "宋体", 14, "#234232");
    ImageUtils.drawPicture(122, 872, 721, 680, frontImg, taobaoImg);
    ImageUtils.drawPicture(907, 872, 721, 680, frontImg, taobaoImg);
    ImageUtils.drawPicture(122, 1610, 721, 680, frontImg, taobaoImg);
    ImageUtils.drawPicture(907, 1610, 721, 680, frontImg, taobaoImg);

    File outFile = new File(resFront);
    ImageIO.write(frontImg, "png", outFile);// 写图片
  }

  private static void drawProduct(int x, int y, BufferedImage ImageNew, BufferedImage ImageTwo) throws IOException {
    int widthTwo = ImageTwo.getWidth();// 图片高度
    int heightTwo = ImageTwo.getHeight();// 图片高度
    int[] ImageArrayTwo = new int[widthTwo * heightTwo];
    ImageArrayTwo = ImageTwo.getRGB(0, 0, widthTwo, heightTwo, ImageArrayTwo, 0, widthTwo);
    ImageNew.setRGB(x, y, widthTwo, heightTwo, ImageArrayTwo, 0, widthTwo);

    URL url = new URL("图片");
    URLConnection conn = url.openConnection();
    InputStream inStream = conn.getInputStream();

    BufferedImage ImagePuduct = ImageIO.read(inStream);
    int widthPuduct = ImagePuduct.getWidth();
    int heightPuduct = ImagePuduct.getHeight();// 图片高度

    Image image = ImagePuduct.getScaledInstance(100, 80, Image.SCALE_DEFAULT);// 获取缩略图
    /* 再创建一个BufferedImage对象 用于创建100*100大小的图像 */
    BufferedImage oimage;
    oimage = new BufferedImage(100, 80, Image.SCALE_DEFAULT);
    /*
     * 获取图像上下文对象，然后把刚才的Image对象画到BufferedImage中去 切忌，
     * drawImage()方法有很多重载方法，一定要选用下面的这个，它会95%的复制原图的图片质量。其他重载方法你也可以试试，可能生成出来的图片很丑噢
     * ~哈哈
     */
    oimage.getGraphics().drawImage(image, 0, 0, null);

    widthPuduct = 100;
    heightPuduct = 80;
    int[] ImageArrayPuduct = new int[widthPuduct * heightPuduct];
    ImageArrayPuduct = oimage.getRGB(0, 0, widthPuduct, heightPuduct, ImageArrayPuduct, 0, widthPuduct);
    ImageNew.setRGB(60, y + 20, widthPuduct, heightPuduct, ImageArrayPuduct, 0, widthPuduct);

    drawProductText(x, y, widthPuduct, heightPuduct, ImageNew);
  }

  private static void drawProductText(int x, int y, int widthProduct, int heightProduct, BufferedImage ImageNew) {
    Graphics g = ImageNew.getGraphics();
    Font f = new Font("", Font.BOLD, 24);
    g.setColor(Color.decode("#333"));
    g.setFont(f);
    g.drawString("内容", x + 60 + widthProduct + 20, y + 40);

    f = new Font("", Font.BOLD, 18);
    g.setFont(f);
    g.setColor(Color.decode("#F4CAC9"));
    g.drawString("内容", 60 + widthProduct + 59, y + 20 + 20 + 32);

    //
    g.dispose();
  }

}
